import { _decorator, renderer, Component, Node, systemEvent, SystemEvent, CanvasComponent, Vec3, LabelComponent} from "cc";
const { ccclass, property } = _decorator;

import { Game } from "./../Game"
import { UICtrl } from "./../../Managers/UIMgr";
import { EventMgr } from "../../Managers/EventMgr";
//特价9.9元一套cocoscreator代码联系Q2483367084 
//截图https://share.weiyun.com/leGAHpnB 密码:b9udtv
@ccclass("MainUI_Ctrl") // 注意修改类名
export class MainUI_Ctrl extends UICtrl {
    private cursor: Node = null;
    private uiCamera: renderer.Camera = null;
    private screenPos: Vec3 = cc.v3(0, 0, 0);
    private outPos: Vec3 = cc.v3();

    onLoad() {
        super.onLoad();
        systemEvent.on(SystemEvent.EventType.TOUCH_MOVE, this.onTouchMove, this);
        
        this.uiCamera = cc.find("Canvas").getComponent(CanvasComponent).camera;
        this.cursor = this.view["cursor"];
        this.add_button_listen("Start", this, this.onStartGame);
        EventMgr.Instance.add_event_listenner("UIEvent_EnterStart", this, this.configStartUI);
        EventMgr.Instance.add_event_listenner("UIEvent_EnterGame", this, this.configGameUI);
        EventMgr.Instance.add_event_listenner("UIEvent_GenEnmey", this, this.onGenEnmeyUI);
    }

    private onGenEnmeyUI(uname, udata): void {
        this.showEnmeyInfo(udata.genNum, udata.desic);
    }

    private onTouchMove(e): void {

        if (this.cursor.active === false) {
            return;
        }

        var delta = e.getDelta(); // 屏幕坐标; Canvas 也会有一个摄像机；
        this.screenPos.x = delta.x;
        this.screenPos.y = delta.y;
        
        this.uiCamera.screenToWorld(this.outPos, this.screenPos);

        var pos = this.cursor.getWorldPosition(); // 世界坐标;
        pos.x += this.outPos.x;
        pos.y += this.outPos.y;
        this.cursor.setWorldPosition(pos);

        this.uiCamera.worldToScreen(this.screenPos, pos);
        EventMgr.Instance.dispatch_event("UICursorMove", this.screenPos);
    }

    private configStartUI(): void { 
        this.view["info_root"].active = false;
        this.view["StartTime"].active = false;
        this.view["MessageTip"].active = false;
        this.view["coin_info"].active = false;
        this.view["cursor"].active = false;

        this.view["Logo"].active = true;
        this.view["Start"].active = true;
    }

    private configGameUI(): void {
        this.view["MessageTip"].active = false;
        this.view["Logo"].active = false;
        this.view["Start"].active = false;

        this.view["info_root"].active = true;
        this.view["StartTime"].active = true;
        this.view["coin_info"].active = true;
        this.view["cursor"].active = true;

        // 同步curosr 位置
        var pos = this.cursor.getWorldPosition(); // 世界坐标;
        this.uiCamera.worldToScreen(this.screenPos, pos);
        EventMgr.Instance.dispatch_event("UICursorMove", this.screenPos);
        // end

        // 倒计时
        this.showTime(3);
        this.scheduleOnce(function() {
            this.showTime(2);
        }.bind(this), 1);

        this.scheduleOnce(function() {
            this.showTime(1);
        }.bind(this), 2);

        this.scheduleOnce(function() {
            this.showTime(0);
        }.bind(this), 3);
        // end 

        // 显示我们出兵的UI信息
        this.showEnmeyInfo(0, null);
        // this.showEnmeyInfo(1, "开始了");
        // end 
    }

    // 如果genNumber = 0, 全部消失
    private showEnmeyInfo(genNumber: number, desic: string) {
        var i = 0;
        genNumber = (genNumber > this.view["info_root"].children.length) ? this.view["info_root"].children.length : genNumber;
        for(i = 0; i < genNumber; i ++) {
            this.view["info_root"].children[i].active = true;
        }

        for(; i < this.view["info_root"].children.length; i ++) {
            this.view["info_root"].children[i].active = false;
        }

        if (genNumber > 0) { // 当前
            desic = (desic) ? desic : "";
            var now: Node = this.view["info_root"].children[genNumber - 1];
            now.getChildByName("order").getChildByName("desic").getComponent(LabelComponent).string = desic;
        }
        
    }

    private showTime(time: number) {
        
        this.view["StartTime"].children[0].active = false;
        this.view["StartTime"].children[1].active = false;
        this.view["StartTime"].children[2].active = false;

        if(time <= 0 || time > 3) {
            return;
        }

        this.view["StartTime"].children[time - 1].active = true;
    }

    onDestroy(): void {
        EventMgr.Instance.remove_event_listenner("UIEvent_EnterStart", this, this.configStartUI);
        EventMgr.Instance.remove_event_listenner("UIEvent_EnterGame", this, this.configGameUI);
        EventMgr.Instance.remove_event_listenner("UIEvent_GenEnmey", this, this.onGenEnmeyUI);
    }
    

    onStartGame(): void {
        Game.Instance.EnterGameMode();
    }
}
